package ticketonline.service.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ticketonline.entity.BookingDetail;
import ticketonline.entity.BookingHeader;
import ticketonline.entity.Customer;
import ticketonline.entity.Ticket;
import ticketonline.service.AppService;

@Service("appService")
@Transactional(readOnly=true)
public class AppServiceImpl implements AppService {
	
	@Autowired private SessionFactory sessionFactory;
	
	@Override
	@Transactional(readOnly=false)
	public void saveTicket(Ticket ticket) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().saveOrUpdate(ticket);
	}

	@Override
	@Transactional(readOnly=false)
	public void deleteTicket(Ticket ticket) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().delete(ticket);
	}

	@Override
	@Transactional(readOnly=false)
	public List<Ticket> findAllTicket() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession()
		.createQuery("FROM Ticket")
		.list();
	}

	@Override
	public Ticket findTicketByKode(String kode) {
		// TODO Auto-generated method stub
		if (kode==null) return null;
		return (Ticket) sessionFactory.getCurrentSession()
		.createQuery("FROM Ticket t WHERE t.kode=:kd")
		.setParameter("kd", kode)
		.uniqueResult();
	}

	@Override
	@Transactional(readOnly=false)
	public void saveCustomer(Customer customer) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().saveOrUpdate(customer);
	}

	@Override
	@Transactional(readOnly=false)
	public void deleteCustomer(Customer customer) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().delete(customer);
	}

	@Override
	public List<Customer> findAllCustomer() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession()
		.createQuery("FROM Customer")
		.list();
	}

	@Override
	public Customer findCustomerByKode(Integer custID) {
		// TODO Auto-generated method stub
		if (custID==null) return null;
		return (Customer) sessionFactory.getCurrentSession()
		.createQuery("FROM Customer c WHERE c.id=:kd")
		.setParameter("kd", custID)
		.uniqueResult();
	}

	@Override
	@Transactional(readOnly=false)
	public void saveTransaction(BookingHeader bookingHeader) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().saveOrUpdate(bookingHeader);
	}

	@Override
	public List<Customer> findAllTransaction() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Customer findTransactionByKode(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Ticket> availableTicket() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession()
		.createSQLQuery("SELECT * FROM tb_ticket WHERE ticket_id NOT IN " 
				+"(SELECT tb_bookingdetail.ticket_id FROM tb_bookingdetail " 
				+ " INNER JOIN tb_ticket ON tb_bookingdetail.ticket_id = tb_ticket.ticket_id)")
		.list();
	}
	
}
